package clothes.tools.web;

import clothes.tools.pojo.Commodity;
import clothes.tools.pojo.ResultInfo;
import clothes.tools.service.CommodityService;
import clothes.tools.service.impl.CommodityServiceImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

public class CommodityServlet extends BaseServlet{

    private CommodityService commodityService=new CommodityServiceImpl();
    /**
     * 商品添加
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType ("application/json;charset=UTF-8");
        String pid = req.getParameter("pid");
        String pname = req.getParameter("pname");
        String pcolor = req.getParameter("pcolor");
        String psize = req.getParameter("psize");
        String price = req.getParameter("price");
        ResultInfo info = new ResultInfo();
        ObjectMapper mapper = new ObjectMapper();
        if(commodityService.existsCommodityId(pid)) {
            info.setFlag(false);
            info.setErrorMsg("商品已存在");
        }
        else {
            commodityService.addCommodity(new Commodity(pid, pname, pcolor, psize, new BigDecimal(price), 0,null));
            info.setFlag(true);
            info.setErrorMsg("添加成功");
        }
        mapper.writeValue(resp.getOutputStream(), info);

    }

    /**
     * 商品删除
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pid = req.getParameter("pid");
        commodityService.deleteCommodityById(pid);
        resp.getWriter().write("success");
    }

    /**
     * 商品信息更新
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType ("text/html;charset=UTF-8");
        String pid=req.getParameter("pid");
        String pname=req.getParameter("pname");
        String pcolor=req.getParameter("pcolor");
        String psize=req.getParameter("psize");
        String price=req.getParameter("price");
        String pnum=req.getParameter("pnum");
        int pnum1=Integer.parseInt(pnum);
        commodityService.updateCommodity(new Commodity(pid,pname,pcolor,psize,new BigDecimal(price),pnum1,null));
        resp.getWriter().write("success");
    }

    /**
     * /商品查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr=req.getParameter("page");
        String limitStr=req.getParameter("limit");
//        1、通过CommodityServlet查询全部商品
        List<Commodity> commodity = commodityService.queryCommodity();
        List<Commodity> commodities=commodityService.queryCommodities(pageStr,limitStr);
        resp.setContentType ("text/html;charset=UTF-8");
        JSONObject jsonObject = new JSONObject ();
        //接收的数据
        JSONArray data = JSONArray.parseArray (JSON.toJSONString (commodities));
        jsonObject.put ("data", data);
        jsonObject.put ("code", 0);
        jsonObject.put ("msg", "");
        //获得数据总数
        jsonObject.put ("count", commodity.size ());
        PrintWriter writer = resp.getWriter ();
        writer.print (jsonObject.toJSONString ());
    }
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String pageStr=req.getParameter("page");
        String limitStr=req.getParameter("limit");
        String select = req.getParameter("select");
        List<Commodity> commodity = commodityService.queryCommodity();
        List<Commodity> commodities=commodityService.queryCommodity(pageStr,limitStr,select);
        resp.setContentType ("text/html;charset=UTF-8");
        JSONObject jsonObject = new JSONObject ();
        JSONArray data = JSONArray.parseArray (JSON.toJSONString (commodities));
        jsonObject.put ("data", data);
        jsonObject.put ("code", 0);
        jsonObject.put ("msg", "");
        jsonObject.put ("count", commodity.size ());
        PrintWriter writer = resp.getWriter ();
        writer.print (jsonObject.toJSONString ());
    }

}
